package com.system.insurance.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.result.Result;
import com.system.insurance.entity.ContentSetting;
import com.system.insurance.entity.SmallInsuranceType;
import com.system.insurance.service.ContentSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 保险内容 前端控制器
 * </p>
 *
 * @author WS
 * @since 2024-05-10
 */
@Api(tags = "保险内容")
@RestController
@RequestMapping("/insurance/content-setting")
public class ContentSettingController {

    @Autowired
    private ContentSettingService contentSettingService;

    @ApiOperation(value = "查看所有保险内容")
    @GetMapping("/contentSettingList")
    public Result<List<ContentSetting>> contentSettingList(){
        List<ContentSetting> list = contentSettingService.list();
        return Result.ok(list);
    }


    @ApiOperation(value = "查看单个保险内容")
    @GetMapping("/contentSettingOne/{id}")
    public Result<ContentSetting> contentSettingOne(@PathVariable Integer id){
        ContentSetting  one= contentSettingService.getById(id);
        return Result.ok(one);
    }

//    @ApiOperation(value = "查看程序单个保险内容")
//    @GetMapping("/contentSettingOne")
//    public Result<ContentSetting> contentSettingOneId(){
//        LambdaQueryWrapper<ContentSetting> wrapper = new LambdaQueryWrapper<>();
//        ContentSetting  one= contentSettingService.getOne(wrapper);
//        return Result.ok(one);
//    }

    @ApiOperation(value = "添加保险内容")
    @PostMapping("/saveContentSetting/{smallId}")
    public Result<ContentSetting> saveContentSetting(@PathVariable Integer smallId, @RequestBody ContentSetting contentSetting){
        contentSetting.setCreateTime(LocalDateTime.now());
        boolean save = contentSettingService.save(contentSetting);

        if (save){
            return Result.ok(contentSetting).message("添加成功");
        }
        return Result.fail(contentSetting).message("添加失败");
    }

    @ApiOperation(value = "修改保险内容")
    @PostMapping("/updateContentSetting")
    public Result<ContentSetting> updateContentSetting(@RequestBody ContentSetting contentSetting){
        contentSetting.setUpdateTime(LocalDateTime.now());
        boolean update = contentSettingService.updateById(contentSetting);

        if (update){
            return Result.ok(contentSetting).message("修改成功");
        }
        return Result.fail(contentSetting).message("修改失败");
    }

    @ApiOperation(value = "删除保险内容")
    @PostMapping("/deleteContentSetting")
    public Result deleteContentSetting(@RequestBody ContentSetting contentSetting){
        boolean remove = contentSettingService.removeById(contentSetting.getId());

        if (remove){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<ContentSetting>> index33(@PathVariable Long page, @PathVariable Long limit, ContentSetting subject){
        Page<ContentSetting> recommendPage = new Page<>(page,limit);
        LambdaQueryWrapper<ContentSetting> wrapper = new LambdaQueryWrapper<>();
        Integer id = subject.getId();
        if (id!=null){
            wrapper.eq(ContentSetting::getId,id);
        }
        IPage<ContentSetting> iPage = contentSettingService.page(recommendPage,wrapper);
        return Result.ok(iPage);
    }

}

